package com.css.fxfzypg.modules.message.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.message.repository.entity.MessageEntity;
import com.css.fxfzypg.modules.message.repository.entity.MessagePageList;
import com.css.fxfzypg.modules.message.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {
    @Autowired
    MessageService messageService;

    @PostMapping("/addMessage")
    public RestResponse addMessage(@RequestBody MessageEntity messageEntity){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            boolean bool = messageService.addMessage(messageEntity);
            jsonObject.put("message", "新增成功");
            restResponse = RestResponse.succeed(bool);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "新增失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("/queryMessage")
    public RestResponse queryMessage(String userId,int currentPage,int pageSize){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        //String userId = "ceshi123";
        try {
            MessagePageList pageList = messageService.queryMessage(userId,currentPage,pageSize);
            jsonObject.put("message", "查询list成功");
            restResponse = RestResponse.succeed(pageList);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "查询list失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    @GetMapping("/getMessageById")
    public RestResponse getMessageById(String messageId){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            MessageEntity messageEntity = messageService.getMessageById(messageId);
            jsonObject.put("message", "查询list成功");
            restResponse = RestResponse.succeed(messageEntity);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "获取失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    @PostMapping("/readMessage")
    public RestResponse readMessage(String messageId){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            boolean bool = messageService.readMessage(messageId);
            jsonObject.put("message", "查看成功");
            restResponse = RestResponse.succeed(bool);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "查看失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}


